<template>
  <div>
    <el-dialog :visible.sync='visible' fullscreen title="合并术语"
               @close='closeDialog'
               @open="openDialog">
      <el-row>
        <!-- 左侧穿梭框 原料框 -->
        <el-col :span="7">
          <div class="transfer-left">
            <div class="transfer-title">
              <span>入库冲突术语</span>
            </div>
            <!-- 内容区 -->
            <div :style="autoHeight" class="transfer-main">
              <div v-for="(dbTerm, idx) in dbTermList">
                <el-radio v-model="leftCheckedValue" :disabled="nodeDisabled[dbTerm.name]" :label="dbTerm.name"
                          style="display: block" @change="leftCheckedNode = dbTerm; leftCheckedType = 'name'">
                  正名: <span v-html="dbTerm.name"/>
                </el-radio>

                <div v-for="(propName, prop) in propMap">
                  <div v-for="(itemValue) in dbTerm[prop]">
                    <el-radio v-if="typeof itemValue === 'object' && UTIL.isNotEmpty(itemValue.value)"
                              v-model="leftCheckedValue" :disabled="nodeDisabled[itemValue.value]"
                              :label="itemValue.value" style="display: block"
                              @change="leftCheckedNode = dbTerm; leftCheckedType = prop">
                      <span v-html="propName + ': ' + itemValue.value"/>
                    </el-radio>
                    <el-radio v-else-if="UTIL.isNotEmpty(itemValue)" v-model="leftCheckedValue"
                              :disabled="nodeDisabled[itemValue]"
                              :label="itemValue" style="display: block"
                              @change="leftCheckedNode = dbTerm; leftCheckedType = prop">
                      <span v-html="propName + ': ' + itemValue"/>
                    </el-radio>
                  </div>
                </div>

                <div v-for="(explainItem) in dbTerm.explanation" v-if="UTIL.isNotEmpty(explainItem.value)">
                  <el-radio v-model="leftCheckedValue" :disabled="nodeDisabled[explainItem.value]"
                            :label="explainItem.value"
                            style="display: block" @change="leftCheckedType = 'explanationSynonym'">
                    释义: <span v-html="explainItem.value"/>
                  </el-radio>
                </div>
                <hr/>
                来源:<span v-html="nameSource"></span>
              </div>
            </div>
          </div>
        </el-col>
        <el-col :offset="1" :span="7">
          <div class="transfer-left">
            <div class="transfer-title">
              <span>标准冲突术语</span>
            </div>
            <!-- 内容区 -->
            <div :style="autoHeight" class="transfer-main">
              <div v-for="(dbTerm, idx) in standardTermList">
                <el-row>
                  <el-button size="mini" style="float: right;" @click="relatedBetweenTerm(dbTerm)">相关术语</el-button>
                </el-row>
                <el-radio v-model="leftCheckedValue" :disabled="nodeDisabled[dbTerm.name]" :label="dbTerm.name"
                          style="display: block" @change="leftCheckedNode = dbTerm; leftCheckedType = 'name'">
                  正名: <span v-html="dbTerm.name"/>
                </el-radio>
                <div v-for="(dbTermSource, idx) in dbTerm.nameSource"
                     style="padding-left: 10px; font-size: 12px;color: #959999;">
                  <!-- 当多本书中含有改正名时,展示优先级最高的图书,后台已经根据图书级别做了排序 -->
                  <span v-html="'《' + dbTermSource.name + '》（' + CONST.TERM_SOURCE[dbTermSource.category] + '）'"></span>
                </div>
                <div v-for="(propName, prop) in propMap">
                  <div v-for="(itemValue) in dbTerm[prop]">
                    <!-- 编辑新建的同义词不能作为正名,标识下 -->
                    <el-radio v-if="typeof itemValue === 'object' && UTIL.isNotEmpty(itemValue.value)"
                              v-model="leftCheckedValue"
                              :disabled="nodeDisabled[itemValue.value]"
                              :label="itemValue.value" style="display: block"
                              @change="leftCheckedNode = dbTerm; leftCheckedType = prop">
                      <span v-html="propName + ': ' + itemValue.value"/>
                      <span v-if="itemValue.type === 1" style="color: red; font-size: 10px;">
                        (*非导入同义词,不能作为正名)
                      </span>
                    </el-radio>
                    <el-radio v-else-if="UTIL.isNotEmpty(itemValue)" v-model="leftCheckedValue"
                              :disabled="nodeDisabled[itemValue]"
                              :label="itemValue" style="display: block"
                              @change="leftCheckedNode = dbTerm; leftCheckedType = prop">
                      <span v-html="propName + ': ' + itemValue"/>
                    </el-radio>
                  </div>
                </div>

                <div v-for="(explainItem) in dbTerm.explanation" v-if="UTIL.isNotEmpty(explainItem.value)">
                  <el-radio v-model="leftCheckedValue" :disabled="nodeDisabled[explainItem.value]"
                            :label="explainItem.value"
                            style="display: block" @change="leftCheckedType = 'explanationSynonym'">
                    释义: <span v-html="explainItem.value"/>
                  </el-radio>
                </div>
                <hr v-if="idx < standardTermList.length -1"/>
              </div>
            </div>
          </div>
        </el-col>

        <el-col :span="2">
          <!-- 穿梭区 按钮框 -->
          <div class="transfer-center">
            <template>
              <p class="transfer-center-item">
                <el-button
                  icon="el-icon-arrow-right"
                  title="应用左侧选中节点"
                  type="primary"
                  @click="mergeToRight"
                ></el-button>
              </p>
              <p class="transfer-center-item">
                <el-button
                  icon="el-icon-arrow-left"
                  title="重新合并"
                  type="primary"
                  @click="redoMerge"
                ></el-button>
              </p>
            </template>
          </div>
        </el-col>
        <el-col :span="7">
          <!-- 右侧穿梭框 目标框 -->
          <div class="transfer-right">
            <div class="transfer-title">
              <span>合并后术语</span>
            </div>
            <!-- 内容区 -->
            <div :style="autoHeight" class="transfer-main">
              <div v-for="(standardTerm, idx) in mergedTermList">
                <el-row align="bottom" type="flex">
                  <el-col :span="20">

                    <el-radio v-model="rightCheckedValue" :label="idx + '.name'" style="display: block"
                              @change="rightCheckedType = 'name'">
                      正名:
                    </el-radio>
                    <div v-if="UTIL.isNotEmpty(standardTerm.name)" style="margin-left: 50px">
                      <span v-if="typeof standardTerm.name === 'object'" v-html="standardTerm.name.value"></span>
                      <span v-else v-html="standardTerm.name"></span>
                      <el-button circle class="inline-button" icon="el-icon-close" size="small" title="删除" type="danger"
                                 @click="standardTerm.name = ''"></el-button>
                    </div>

                    <div v-for="(propName, prop) in propMap">
                      <el-radio v-model="rightCheckedValue" :label="idx + '.' + prop" style="display: block"
                                @change="rightCheckedType = prop">
                        <span v-html="propName + ': '"/>
                      </el-radio>
                      <div v-for="(itemValue, itemIdx) in standardTerm[prop]" style="margin-left: 50px">
                        <span v-if="typeof itemValue === 'object'" v-html="itemValue.value"></span>
                        <span v-else v-html="itemValue"></span>
                        <el-button circle class="inline-button" icon="el-icon-close" size="small" title="删除"
                                   type="danger"
                                   @click="standardTerm[prop].splice(itemIdx, 1)"></el-button>
                      </div>
                    </div>

                    <el-radio v-model="rightCheckedValue" :label="idx + '.explanation'" style="display: block"
                              @change="rightCheckedType = 'explanationSynonym'">
                      释义:
                    </el-radio>
                    <div v-for="(explainItem, explainIdx) in standardTerm.explanation"
                         v-if="UTIL.isNotEmpty(explainItem.value)"
                         style="margin-left: 50px">
                      <span v-html="explainItem.value"></span>
                      <el-button circle class="inline-button" icon="el-icon-close" size="small" title="删除" type="danger"
                                 @click="standardTerm.explanation.splice(explainIdx, 1)"></el-button>
                    </div>
                  </el-col>
                  <el-col :span="4">
                    <el-button plain type="danger" @click="mergedTermList.splice(idx, 1)">删除术语</el-button>
                  </el-col>
                </el-row>
                <hr/>
              </div>
              <el-button icon="el-icon-plus" style="display: flex; margin: 10px auto; " @click="addStandardTerm">
                新建标准术语
              </el-button>
            </div>
          </div>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
    <el-drawer v-if="dialogVisible" :size="CONST.WORKSPACE_HEIGHT + 'px'" :visible="dialogVisible"
               title="详情"
               @close="dialogVisible = false">
      <RelatedBetweenTerm :relatedTerm=relatedTerm></RelatedBetweenTerm>
    </el-drawer>
  </div>
</template>

<script>
import RelatedBetweenTerm from "./RelatedBetweenTerm";

export default {
  name: 'SolveStandardTermConflictDialog',
  components: {RelatedBetweenTerm},
  props: ['conflictTermList', 'termBookMetadataId'],
  data() {
    return {
      leftCheckedType: '',
      leftCheckedValue: '',
      leftCheckedNode: {},
      rightCheckedType: '',
      rightCheckedValue: '',
      dbTermList: [],
      standardTermList: [],
      mergedTermList: [],
      propMap: {
        nameSynonym: '同义词',
        englishName: '英文名称',
        englishNameSynonym: '英文名称同义词',
        englishAbbr: '英文简称',
        englishAbbrSynonym: '英文简称同义词',
        frenchName: '法文名称',
        frenchNameSynonym: '法文名称同义词',
        russianName: '俄文名称',
        russianNameSynonym: '俄文名称同义词',
        germanName: '德文名称',
        germanNameSynonym: '德文名称同义词',
        japaneseName: '日文名称',
        japaneseNameSynonym: '日文名称同义词'
      },
      nameSource: '',
      visible: false,
      nodeDisabled: {},
      mergedTermValueSet: [],
      autoHeight: {
        height: CONST.WORKSPACE_HEIGHT - 190 + 'px',
      },
      deepCopyConflictTermList: [],
      deepCopyConflictStandardList: [],
      dialogVisible: false,
      relatedTerm: []
    }
  },
  methods: {
    redoMerge() {
      this.openDialog()
    },
    mergeToRight() {
      if (!UTIL.isNotEmpty(this.leftCheckedValue) || !UTIL.isNotEmpty(this.rightCheckedValue)) {
        this.$message.warning("两侧必须选中相应节点才能赋值")
        return
      }
      if (this.leftCheckedType.replace('Synonym', '') !== this.rightCheckedType.replace('Synonym', '')) {
        // 英文名称, 英文简称 能相互赋值
        if (!this.leftCheckedType.startsWith('english')
          || !this.rightCheckedType.startsWith('english')) {
          this.$message.warning("左右侧选中节点数据不一致, 不能赋值")
          return
        }
      }
      let checkedStandardTerm = this.mergedTermList[this.rightCheckedValue.split(".")[0]]
      // 右侧做为正名时, 连带分类;
      if (this.rightCheckedType === 'name') {
        checkedStandardTerm.name = this.leftCheckedValue
        this.mergedTermValueSet.push(this.leftCheckedValue)
        checkedStandardTerm.xmlClassify = this.leftCheckedNode.xmlClassify
        // 左侧为正名时, 连带其它属性;
        if (this.leftCheckedType === 'name') {
          for (let propName in this.leftCheckedNode) {
            if (propName === 'nameSource' || propName === 'synonym') {
              continue;
            }
            // 除了名称外, 其它需要处理的全是数组
            if (Array.isArray(this.leftCheckedNode[propName])) {
              for (let item of this.leftCheckedNode[propName]) {
                // 如果右侧未使用, 则添加到右侧
                if (this.mergedTermValueSet.indexOf((typeof item === 'object') ? item.value : item) === -1) {
                  checkedStandardTerm[propName].push(item)
                  this.mergedTermValueSet.push((typeof item === 'object') ? item.value : item)
                }
              }
            }
          }
        }
      } else if (this.rightCheckedType === 'explanationSynonym') {
        checkedStandardTerm.explanation.push({value: this.leftCheckedValue})
      }
      // 同义词;
      else if (this.rightCheckedType === 'nameSynonym') {
        checkedStandardTerm.nameSynonym.push({value: this.leftCheckedValue})
      }
      // 复制其它属性, 直接赋值;
      else {
        checkedStandardTerm[this.rightCheckedValue.split(".")[1]].pushIfNotContains(this.leftCheckedValue)
      }
      // 计算 来源;
      // 正名 同义词;
      // 合并后术语作为正名时
      // 正名来源保留级别最高的来源,如果最高级别有多个,都保留
      if (this.rightCheckedType === 'name') {
        // 冲突的入库术语
        let tmpNameSource = []
        for (let dbTerm of this.dbTermList) {
          if (dbTerm.name === this.leftCheckedValue) {
            tmpNameSource.push(dbTerm.nameSource[0])
            break
          }
        }
        // 冲突的标准术语
        for (let standardTerm of this.standardTermList) {
          if (standardTerm.name === this.leftCheckedValue) {
            // 左侧标准术语正名， 作为右侧标准术语正名， 保留id；
            checkedStandardTerm.id = standardTerm.id
            checkedStandardTerm.version = standardTerm.version
            tmpNameSource = tmpNameSource.concat(standardTerm.nameSource)
            break
          }
          // 标准术语的同义词做正名
          for (let synonym of standardTerm.nameSynonym) {
            if (synonym.value === this.leftCheckedValue) {
              let tmpNameSourceItem = JSON.parse(JSON.stringify(synonym))
              delete tmpNameSourceItem.value
              tmpNameSource.push(tmpNameSourceItem)
              break
            }
          }
        }
        // 排序;
        tmpNameSource.sort((item1, item2) => item1.category - item2.category)
        // 去重;
        tmpNameSource = UTIL.distinctByProp(tmpNameSource, 'isbn')
        // 取级别最高的前n个;
        checkedStandardTerm.nameSource = tmpNameSource.filter(item => item.category === tmpNameSource[0].category)
      }
      // 同义词, 计算来源
      let concatList = this.dbTermList.concat(this.standardTermList);
      for (let i = 0; i < checkedStandardTerm.nameSynonym.length; i++) {
        // 合并后的同义词
        let synonymItem = checkedStandardTerm.nameSynonym[i]
        let synonymItemSource = []
        // 根据同义词 value 查找原集合；
        for (let dbTerm of concatList) {
          if (synonymItem.value === dbTerm.name) {
            let tmpNameSourceObj = JSON.parse(JSON.stringify(dbTerm.nameSource[0]));
            tmpNameSourceObj.value = dbTerm.name;
            synonymItemSource.push(tmpNameSourceObj);
          }
          for (let dbTermSynonymItem of dbTerm.nameSynonym) {
            if (synonymItem.value === dbTermSynonymItem.value) {
              synonymItemSource.push(dbTermSynonymItem)
            }
          }
        }
        // 遍历同义词的来源，取级别最高的一个
        // 根据级别排序
        synonymItemSource.sort((item1, item2) => item1.category - item2.category)
        checkedStandardTerm.nameSynonym[i] = synonymItemSource[0]
      }

      // 释义， 计算释义来源；
      for (let i = 0; i < checkedStandardTerm.explanation.length; i++) {
        let explainItem = checkedStandardTerm.explanation[i]
        let explainItemSource = []
        // 根据释义value 查找原集合；
        for (let dbTerm of concatList) {
          for (let dbTermExplainItem of dbTerm.explanation) {
            if (explainItem.value === dbTermExplainItem.value) {
              explainItemSource.push(dbTermExplainItem)
            }
          }
        }
        // 遍历释义的来源， 如果和当前正名的来源一致， 则直接使用；
        for (let tmpSource of explainItemSource) {
          if (checkedStandardTerm.nameSource.find(item => item.isbn === tmpSource.isbn)) {
            checkedStandardTerm.explanation[i] = tmpSource
          } else {
            checkedStandardTerm.explanation[i] = explainItemSource[0]
          }
        }
      }
      // 释义排序， 跟正名一致的排前面， 不一致的按级别排
      let sameSourceExplain = checkedStandardTerm.explanation.filter(explainItem => checkedStandardTerm.nameSource.find(item => item.isbn === explainItem.isbn))
      // 正名一致的,根据级别排序
      sameSourceExplain.sort((item1, item2) => item1.category - item2.category)
      // 不一致的；
      let notSameSourceExplain = checkedStandardTerm.explanation.filter(explainItem => !sameSourceExplain.includes(explainItem)).sort((item1, item2) => item1.category - item2.category)
      sameSourceExplain = sameSourceExplain.concat(notSameSourceExplain)
      checkedStandardTerm.explanation = sameSourceExplain

      this.leftCheckedValue = ''
      this.leftCheckedType = ''
    },
    addStandardTerm() {
      this.mergedTermList.push({
        name: '',
        nameSynonym: [],
        englishName: [],
        englishNameSynonym: [],
        englishAbbr: [],
        englishAbbrSynonym: [],
        frenchName: [],
        frenchNameSynonym: [],
        russianName: [],
        russianNameSynonym: [],
        germanName: [],
        germanNameSynonym: [],
        japaneseName: [],
        japaneseNameSynonym: [],
        explanation: []
      })
    },
    submit() {
      // 校验右侧合并后的数据, 正名是否都有;
      for (let term of this.mergedTermList) {
        if (!term.name) {
          this.$message.warning("标准术语, 名称不能为空")
          return
        }
      }
      // 深拷贝一份, 不影响页面展示数据;
      let mergedTermListBak = JSON.parse(JSON.stringify(this.mergedTermList))
      for (let standardTerm of mergedTermListBak) {
        standardTerm.synonym = standardTerm.nameSynonym
        standardTerm.englishNameSynonym = standardTerm.englishNameSynonym.join(";")
        standardTerm.englishAbbrSynonym = standardTerm.englishAbbrSynonym.join(";")
        standardTerm.frenchNameSynonym = standardTerm.frenchNameSynonym.join(";")
        standardTerm.russianNameSynonym = standardTerm.russianNameSynonym.join(";")
        standardTerm.germanNameSynonym = standardTerm.germanNameSynonym.join(";")
        standardTerm.japaneseNameSynonym = standardTerm.japaneseNameSynonym.join(";")
        standardTerm.englishName = standardTerm.englishName.join(";")
        standardTerm.englishAbbr = standardTerm.englishAbbr.join(";")
        standardTerm.frenchName = standardTerm.frenchName.join(";")
        standardTerm.russianName = standardTerm.russianName.join(";")
        standardTerm.germanName = standardTerm.germanName.join(";")
        standardTerm.japaneseName = standardTerm.japaneseName.join(";")
      }
      this.axios.post('/pmph/action/term/solveConflictStandardTerm', {
        "standardTermList": mergedTermListBak,
        "conflictConceptId": this.conflictTermList[0].conflictConceptId,
        "termBookMetadataId": this.termBookMetadataId,
        "conflictBetweenList": this.deepCopyConflictTermList,
        "conflictStandardList": this.deepCopyConflictStandardList
      }).then(() => {
        this.$emit('merged')
        this.closeDialog()
      })
    },

    openDialog() {
      this.deepCopyConflictTermList = JSON.parse(JSON.stringify(this.conflictTermList))
      // 把入库冲突术语的同义词和释义转为与标准术语一致的数据结构,方便页面解析
      for (let dbTerm of this.deepCopyConflictTermList) {
        dbTerm.nameSynonym = [];
        for (let synonym of dbTerm.synonym.split(";").filter(UTIL.isNotEmpty)) {
          let tmpSynonym = JSON.parse(JSON.stringify(dbTerm.nameSource[0]));
          tmpSynonym.value = synonym;
          dbTerm.nameSynonym.push(tmpSynonym);
        }
        for (let i = 0; i < dbTerm.explanation.length; i++) {
          let tmpExplain = JSON.parse(JSON.stringify(dbTerm.nameSource[0]));
          tmpExplain.value = dbTerm.explanation[i];
          dbTerm.explanation[i] = tmpExplain;
        }
      }

      this.dbTermList = JSON.parse(JSON.stringify(this.deepCopyConflictTermList));
      // 冲突入库术语来源
      this.nameSource = '《' + this.dbTermList[0].nameSource[0].name + '》（' +
        CONST.TERM_SOURCE[this.dbTermList[0].nameSource[0].category] + '）';
      for (let dbTerm of this.dbTermList) {
        dbTerm.englishNameSynonym = dbTerm.englishNameSynonym.split(";").filter(UTIL.isNotEmpty)
        dbTerm.englishAbbrSynonym = dbTerm.englishAbbrSynonym.split(";").filter(UTIL.isNotEmpty)
        dbTerm.frenchNameSynonym = dbTerm.frenchNameSynonym.split(";").filter(UTIL.isNotEmpty)
        dbTerm.russianNameSynonym = dbTerm.russianNameSynonym.split(";").filter(UTIL.isNotEmpty)
        dbTerm.germanNameSynonym = dbTerm.germanNameSynonym.split(";").filter(UTIL.isNotEmpty)
        dbTerm.japaneseNameSynonym = dbTerm.japaneseNameSynonym.split(";").filter(UTIL.isNotEmpty)
        dbTerm.englishName = dbTerm.englishName.split(";").filter(UTIL.isNotEmpty)
        dbTerm.englishAbbr = dbTerm.englishAbbr.split(";").filter(UTIL.isNotEmpty)
        dbTerm.frenchName = dbTerm.frenchName.split(";").filter(UTIL.isNotEmpty)
        dbTerm.russianName = dbTerm.russianName.split(";").filter(UTIL.isNotEmpty)
        dbTerm.germanName = dbTerm.germanName.split(";").filter(UTIL.isNotEmpty)
        dbTerm.japaneseName = dbTerm.japaneseName.split(";").filter(UTIL.isNotEmpty)
      }
      this.leftCheckedValue = ''
      this.leftCheckedType = ''
      this.rightCheckedType = ''
      this.rightCheckedValue = ''
      this.nodeDisabled = {}
      // 页面初始化, 设置左侧disabled属性;
      for (let dbTerm of this.dbTermList) {
        this.$set(this.nodeDisabled, dbTerm.name, false)
        // 其它已用过的属性;
        for (let propName in dbTerm) {
          if (propName === 'nameSource' || propName === 'synonym') {
            continue;
          }
          if (Array.isArray(dbTerm[propName])) {
            for (let item of dbTerm[propName]) {
              if (typeof item === 'object') {
                this.$set(this.nodeDisabled, item.value, false);
                continue;
              }
              this.$set(this.nodeDisabled, item, false)
            }
          }
        }
      }

      // 查询当前conflictId冲突的标准术语;
      this.axios.get('/pmph/action/term/selectStandardTermByConflictId', {
        params: {
          conflictId: this.conflictTermList[0].conflictConceptId
        }
      }).then((response) => {
        this.deepCopyConflictStandardList = JSON.parse(JSON.stringify(response.data));
        this.mergedTermList = []
        for (let standardTerm of response.data) {
          standardTerm.nameSynonym = standardTerm.synonym
          standardTerm.englishNameSynonym = standardTerm.englishNameSynonym.split(";").filter(UTIL.isNotEmpty)
          standardTerm.englishAbbrSynonym = standardTerm.englishAbbrSynonym.split(";").filter(UTIL.isNotEmpty)
          standardTerm.frenchNameSynonym = standardTerm.frenchNameSynonym.split(";").filter(UTIL.isNotEmpty)
          standardTerm.russianNameSynonym = standardTerm.russianNameSynonym.split(";").filter(UTIL.isNotEmpty)
          standardTerm.germanNameSynonym = standardTerm.germanNameSynonym.split(";").filter(UTIL.isNotEmpty)
          standardTerm.japaneseNameSynonym = standardTerm.japaneseNameSynonym.split(";").filter(UTIL.isNotEmpty)
          standardTerm.englishName = standardTerm.englishName.split(";").filter(UTIL.isNotEmpty)
          standardTerm.englishAbbr = standardTerm.englishAbbr.split(";").filter(UTIL.isNotEmpty)
          standardTerm.frenchName = standardTerm.frenchName.split(";").filter(UTIL.isNotEmpty)
          standardTerm.russianName = standardTerm.russianName.split(";").filter(UTIL.isNotEmpty)
          standardTerm.germanName = standardTerm.germanName.split(";").filter(UTIL.isNotEmpty)
          standardTerm.japaneseName = standardTerm.japaneseName.split(";").filter(UTIL.isNotEmpty)
          this.mergedTermList.push(standardTerm)
        }
        this.standardTermList = JSON.parse(JSON.stringify(this.mergedTermList))
        // 自动合并的术语处理正名来源
        // 判断入库冲突术语的正名,或者同义词与合并后的术语正名是否相同,如果相同,则比较来源级别,取最高的保留
        // 冲突入库术语正名和同义词集合
        // 避免释义和同义词,正名重复,声明两个数组
        let betweenNameAndSynonym = [];
        let betweenExplain = [];
        let betweenNameSource = this.deepCopyConflictTermList[0].nameSource[0];
        for (let conflictBetween of this.deepCopyConflictTermList) {
          betweenNameAndSynonym.push(conflictBetween.name);
          conflictBetween.nameSynonym.forEach(item => {
            betweenNameAndSynonym.push(item.value);
          });
          conflictBetween.explanation.forEach(item => {
            betweenExplain.push(item.value);
          });
        }
        for (let i = 0; i < this.mergedTermList.length; i++) {
          // 查找合并后的正名,同义词,释义是否包含在冲突术语中,如果包含,则判断来源级别,取级别最高的数据
          if (betweenNameAndSynonym.indexOf(this.mergedTermList[i].name) !== -1) {
            // 如果级别相同,则追加
            if (this.mergedTermList[i].nameSource[0].category === betweenNameSource.category) {
              this.mergedTermList[i].nameSource.push(betweenNameSource);
            } else if (this.mergedTermList[i].nameSource[0].category > betweenNameSource.category) {
              // 如果级别大于,则替换,category越小,级别越高
              let tmpArray = [];
              tmpArray.push(betweenNameSource);
              this.mergedTermList[i].nameSource = tmpArray;
            }
            // 否则不予处理
          }
          // 同样的逻辑处理同义词
          for (let j = 0; j < this.mergedTermList[i].nameSynonym.length; j++) {
            if (betweenNameAndSynonym.indexOf(this.mergedTermList[i].nameSynonym[j].value) !== -1) {
              // 如果级别大于,则替换,category越小,级别越高
              if (this.mergedTermList[i].nameSynonym[j].category > betweenNameSource.category) {
                let tmpBetweenNameSource = JSON.parse(JSON.stringify(betweenNameSource));
                tmpBetweenNameSource.value = this.mergedTermList[i].nameSynonym[j].value;
                this.mergedTermList[i].nameSynonym[j] = tmpBetweenNameSource;
              }
            }
            // 否则不予处理
          }
          // 同样的逻辑处理释义
          for (let j = 0; j < this.mergedTermList[i].explanation.length; j++) {
            if (betweenExplain.indexOf(this.mergedTermList[i].explanation[j].value) !== -1) {
              // 如果级别大于,则替换,category越小,级别越高
              if (this.mergedTermList[i].explanation[j].category > betweenNameSource.category) {
                let tmpBetweenNameSource = JSON.parse(JSON.stringify(betweenNameSource));
                tmpBetweenNameSource.value = this.mergedTermList[i].explanation[j].value;
                this.mergedTermList[i].explanation[j] = tmpBetweenNameSource;
              }
            }
            // 否则不予处理
          }
          // 释义排序
          // 释义排序， 跟正名一致的排前面， 不一致的按级别排
          let sameSourceExplain = this.mergedTermList[i].explanation.filter(explainItem => this.mergedTermList[i].nameSource.find(item => item.isbn === explainItem.isbn))
          // 正名一致的,根据级别排序
          sameSourceExplain.sort((item1, item2) => item1.category - item2.category)
          // 不一致的；
          let notSameSourceExplain = this.mergedTermList[i].explanation.filter(explainItem => !sameSourceExplain.includes(explainItem)).sort((item1, item2) => item1.category - item2.category)
          sameSourceExplain = sameSourceExplain.concat(notSameSourceExplain)
          this.mergedTermList[i].explanation = sameSourceExplain
        }
      })
    },
    closeDialog() {
      this.standardTermList = []
      this.visible = false
    },
    relatedBetweenTerm: function (obj) {
      let tmpObj = [];
      tmpObj.push(obj.name);
      tmpObj = tmpObj.concat(obj.synonym.map(item => item.value))
      this.relatedTerm = tmpObj;
      this.dialogVisible = true;
    },
  },
  watch: {
    mergedTermList: {
      handler() {
        // 把nodeDisabled属性全置为false
        for (let prop in this.nodeDisabled) {
          this.nodeDisabled[prop] = false
        }
        // 重新组织valueSet;
        this.mergedTermValueSet = []
        for (let mergedTerm of this.mergedTermList) {
          // 新建标准术语时,正名为空,不向mergedTermValueSet存值
          if (UTIL.isNotEmpty(mergedTerm.name)) {
            this.mergedTermValueSet.push(mergedTerm.name)
          }
          for (let propName in mergedTerm) {
            if (Array.isArray(mergedTerm[propName])) {
              for (let item of mergedTerm[propName]) {
                if (typeof item === 'object') {
                  this.mergedTermValueSet.push(item.value);
                  continue;
                }
                this.mergedTermValueSet.push(item)
              }
            }
          }
        }
        // 右侧已经引用的值, 设置disabled;
        for (let item of this.mergedTermValueSet) {
          this.nodeDisabled[item] = true
        }
      },
      deep: true
    },
  }
}
</script>

<style scoped>
/deep/ .el-dialog__body {
  padding: 0px 20px;
}

.el-radio {
  line-height: 30px;
}

.inline-button {
  display: inline-block;
  width: 16px;
  height: 16px;
  border-radius: 8px;
  padding: 0;
  margin: 0;
}

.transfer-main {
  overflow: auto;
  height: 470px;
  margin: 5px 0px 5px 10px;
  border-bottom-left-radius: 5px;
  border-bottom-right-radius: 5px;
}

.transfer-left, .transfer-right {
  border: 1px solid #C9C9C9;
  border-radius: 5px;
}

.transfer-center {
  margin-top: 100px;
  text-align: center;
}

.transfer-title {
  background: #F0F8F8;
  font-size: 20px;
  padding: 10px;
  border-top-left-radius: 5px;
  border-top-right-radius: 5px;
}

</style>
